package mindteck.store;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OrderDAO extends BaseDAO{
	
	private final String ORDERINFOBYORDERIDQUERY = "SELECT * FROM ORDERS WHERE ORDERID = ?"; 
	private final String INSERTORDERINFO = "INSERT INTO ORDERS VALUES (?,?,?,?,?,?,?,?,?)";
	
	
	public OrderBean getOrderInfoByOrderId(int orderId){
		OrderBean orderBean = null;
		PreparedStatement pst = null;
		Connection connection = null;
		
		try{
			connection = getPoolOfConnections();
			pst = connection.prepareStatement(ORDERINFOBYORDERIDQUERY);
			pst.setInt(1,orderId);
			ResultSet rs = pst.executeQuery();
			
			if(rs.next()){
				orderBean  = new OrderBean();
				orderBean.setCustomerId(rs.getInt("customerId"));
				orderBean.setDateCreated(rs.getString("dateCreated"));
				orderBean.setDateShipped(rs.getString("dateShipped"));
				orderBean.setShippingId(rs.getInt("shippingId"));
				orderBean.setOrderId(orderId);
				orderBean.setStatus(rs.getString("status"));
				orderBean.setTotal(rs.getDouble("total"));
				orderBean.setQuantity(rs.getInt("quantity"));
				orderBean.setProductId(rs.getInt("productId"));
				
			}
			
			
		}catch(SQLException exp){
			exp.printStackTrace();
		}finally{
			if(connection != null){
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
		
		
		return orderBean;
	}
	
	public boolean insertOrderInfo(OrderBean orderBean){
		PreparedStatement pst = null;
		Connection con = null;
		boolean t = true;
		
		try{
			con  = getPoolOfConnections();
			pst = con.prepareStatement(INSERTORDERINFO);
			
			/*If exception occurs, set boolean to false*/
		}catch(SQLException sql){
			sql.printStackTrace();
			t = false;
		}finally{
			if(con != null){
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return t;
	}
	
	

}
